

[CmdletBinding(PositionalBinding=$false)]
Param(
  [Parameter(Mandatory)]
  $TargetGDBServerPort,
  [Parameter(Mandatory)]
  $TargetADBUSBSerialNumber,
  [Parameter(Mandatory)]
  $ExecPath,
  [Parameter(ValueFromRemainingArguments=$true)]
  [string[]]$ParamList=$null
)

$ADBArgs=""

if($TargetADBUSBSerialNumber.length -gt 0){
  $ADBArgs=$ADBArgs+"-s ${TargetADBUSBSerialNumber} "
}

$ADBArgArr=("${ADBArgs}shell" -split ' ')

$ArgumentList=${ADBArgArr}

$ArgumentList+="gdbserver"
$ArgumentList+=":${TargetGDBServerPort}"
$ArgumentList+="${ExecPath}"
foreach($item in $ParamList){
  $ArgumentList+=$item
}


adb ("${ADBArgs} forward" -split ' ') tcp:${TargetGDBServerPort} tcp:${TargetGDBServerPort}
if($LASTEXITCODE -ne 0){
  exit 1
}


Start-Process adb -ArgumentList $ArgumentList

